<?php
/**
 * Created by PhpStorm.
 * User: lhl
 * Date: 2020/10/21
 * Time: 11:13
 * Features: 安装检测
 */

namespace app\adminapiv3\middleware;


use app\adminapiv3\service\Method;
use app\common\model\FilePathModel;
use app\common\service\ApiHelper;
use think\Request;

class CheckInstall
{
    //安装检测忽略控制器
    const ignoreController = [
        'install',
        'test',
        'index'
    ];

    //响应码
    private $responseCode = [];

    public function handle(Request $request, \Closure $next)
    {
        //状态码
        $this->responseCode = ApiHelper::getCode();

        //真实路由 = 控制器/方法
        $controller = strtolower($request->controller());
        $action = strtolower($request->action());
        $route = $controller.'/'.$action;

        //忽略检测的控制器
        if(in_array($controller, self::ignoreController)){
            return $next($request);
        }

        //检测是否安装
        $methodServer = new Method();
        //安装检测忽略路由
        $noInstall = $methodServer->noInstall();
        if (in_array($route, $noInstall)) {
            return $next($request);
        }

        $filePathModel = new FilePathModel();
        $installFileName = $filePathModel->install_path;
        $systemFileName = $filePathModel->system_time;
        if (!file_exists($installFileName)  || !file_exists($systemFileName)) {
            returnErrJson($this->responseCode['not_install_error'], '该系统未安装,请先完成安装');
        }
        return $next($request);
    }

}